package com.zcl.servlet;

import com.zcl.entity.*;
import com.alibaba.fastjson.JSON;
import com.zcl.entity.R;
import com.zcl.service.WorkerService;
import com.zcl.service.WorkerServiceImpl;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/worker")
public class WorkerServlet  extends HttpServlet {
    private WorkerService workerService;

    @Override
    public void init() throws ServletException {
        workerService=new WorkerServiceImpl();
    }

    @Override
    public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        //字符集编码
        res.setContentType("text/html;charset=utf8");
        //根据不同的值调用不同的方法
        String cmd = req.getParameter("cmd");
        //判断空值
        if (StringUtils.isNoneBlank(cmd)){
            if ("list".equals(cmd)){
                //列表查询
                list(req,res);
            }else if("add".equals(cmd)){
                //添加员工
                add(req,res);
            }else if ("search".equals(cmd)){
                //分页查询
                search(req,res);
            }else if ("findByPage".equals(cmd)){
                findByPage(req,res);
            }
        }
    }

    private void findByPage(ServletRequest req, ServletResponse res) throws IOException {
        //得到分页的参数
        String pageStr = req.getParameter("page");
        String pageSizeStr = req.getParameter("pageSize");
        //两个参数转化成int类型
        int page = new Integer(pageStr);
        int pageSize = new Integer(pageSizeStr);
        //查询当前页的数据
        PageResult<worker> pr=workerService.findByPage(page,pageSize);
        //将结果集放入R
        R r = R.ok().put("pr", pr);
        String s = JSON.toJSONString(r);
        res.getWriter().println(s);
        res.getWriter().flush();

    }

    private void search(ServletRequest req, ServletResponse res) throws IOException {
        //得到分页的参数
         String pageStr = req.getParameter("page");
         String pageSizeStr = req.getParameter("pageSize");
//         两个参数转化成int类型
         int page = new Integer(pageStr);
         int pageSize = new Integer(pageSizeStr);
         //得到分页查询对象
         String s = req.getParameter("work");
         //转化成为java对象
         WorkerVo workerVo = JSON.parseObject(s, WorkerVo.class);
         PageResult<worker> pr=workerService.search(page,pageSize,workerVo);
        //将Pr对象放入R对象结果集
        R r = R.ok().put("pr", pr);
        //将结果集对象转化成为字符串
        String s1 = JSON.toJSONString(r);
        //输出到页面
        res.getWriter().println(s1);
        res.getWriter().flush();
    }

    //添加员工
    private void add(ServletRequest req, ServletResponse res) throws IOException {
        //得到数据
        String work = req.getParameter("work");
        //转化成为员工对象
        worker worker = JSON.parseObject(work, worker.class);
        //保存对象
        workerService.save(worker);
        R r = R.ok().put("message", "添加成功");
        res.getWriter().println(JSON.toJSONString(r));
        res.getWriter().flush();
    }

    //列表查询
    private void list(ServletRequest req, ServletResponse res) throws IOException {
        //查询所有员工列表
        List<worker> workers= workerService.findALL();
        //员工对象放入结果集
        R data = R.ok().put("list", workers);
        //转化成为JSON字符串
        String s = JSON.toJSONString(data);
        //页面输出
        res.getWriter().println(s);
        res.getWriter().flush();
    }
//添加员工

}
